# -*- coding: utf-8 -*-

'''eof
name:企业法定代表人是否为股东校验
code:R10_EG_ShareholderCheck
tableName:
columnName:
groups:企业业务信息校验
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import pandas as pd
import json
from datetime import datetime
# reload(sys)
# sys.setdefaultencoding('utf-8')


def FormatData(tableName):
    try:
        eg = EG_DSJ.get("data").get("R1103V3")[0].get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]


def getRule10():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    shareHolderList = FormatData("shareHolderList")
    legalPerson = ratingRequest.get("legalPerson")
    basic_list = FormatData("basicList")

    # 20190514新增逻辑，当为个人独资企业是通过
    if basic_list == [{}]:
        pass
    else:
        if len(basic_list) >= 1:
            enterprise_type = basic_list[0].get("enterpriseType")
            if u"个人独资企业" == enterprise_type:
                return 1

    if shareHolderList == [{}]:
        return u'缺失值'
    else:
        if len(shareHolderList) >= 1:
            resultList = []
            nameTotal = 0
            for i in shareHolderList:
                shareholderName = i.get("shareholderName")
                if shareholderName not in null_type_list and u"公司" in shareholderName and len(shareholderName) > 5:
                    nameTotal += 1
                if shareholderName not in null_type_list and shareholderName == legalPerson:
                    resultList.append(1)

            # 20190426新增逻辑，所有股东名称全包含公司则返回1
            if len(shareHolderList) == nameTotal:
                return 1
            if sum(resultList) > 0:
                return 1
            else:
                return 0
        else:
            return 1


if __name__ == "__main__":
    ratingRequest = {"legalPerson": u"朱为广", "legalPersonIdCard": "510102196706108511"}
    file_obj = open(r'../data/rating/R10_EG_ShareholderCheck/EG_DSJ.json', 'r')
    content = file_obj.read()
    EG_DSJ = json.loads(content, strict=False)
    aa = getRule10()
    print aa

result = getRule10()

